#region Copyright RenGuiYou. All rights reserved.
//=====================================================
// NeatlyFrameWork
// Author:      RenGuiyou
// Feedback: 	mailto:750539605@qq.com
//=====================================================
#endregion
using System.Collections.Generic;
using Neatly.Load;
using Neatly.Module;
using UnityEngine;

namespace Pandora.Console
{
    public class PoolWindowBase : ScrollWindowBase
    {
        protected enum PoolType
        {
            AssetBundle,
            Atlas
        }

        protected PoolType m_Type;
        private Dictionary<string, Loader> m_AllLoaders;
        private List<Loader> m_TempLoaders;
        private List<Loader> m_PersistentLoaders;
        protected string topHint;

        protected override void OnDrawScrollableWindow()
        {
            GUILayout.Label(topHint);
            GUILayout.BeginVertical("box");
            {
                if (GUILayout.Button("刷新"))
                {
                    TakeSample();
                }
                GUILayout.Space(10);
                float curTime = Time.realtimeSinceStartup - NeatlyConfig.TIME_BUNDLE_CACHE;
                bool needRefresh = false;
                if (m_TempLoaders != null && m_TempLoaders.Count > 0)
                {
                    GUILayout.Label("<b>============================临时资源============================</b>");
                    GUILayout.BeginHorizontal();
                    {
                        GUILayout.Label("<b>名字</b>", GUILayout.Width(240));
                        GUILayout.Label("<b>依赖数量</b>", GUILayout.Width(120));
                        GUILayout.Label("<b>引用数量</b>", GUILayout.Width(120));
                        GUILayout.Label("<b>缓存倒计时</b>", GUILayout.Width(100));
                    }
                    GUILayout.EndHorizontal();
                    foreach (Loader loader in m_TempLoaders)
                    {
                        int countDown = (int)(loader.LiveTime - curTime);
                        if (countDown <= 0)
                            needRefresh = true;
                        GUILayout.BeginHorizontal();
                        {
                            GUILayout.Label(loader.Path, GUILayout.Width(240));
                            GUILayout.Label(loader.DependCount.ToString(), GUILayout.Width(120));
                            GUILayout.Label(loader.RefCount.ToString(), GUILayout.Width(120));
                            var secondLabel = "--";
                            if (loader.RefCount == 0 && loader.DependCount == 0)
                            {
                                secondLabel = $"{countDown}秒";
                            }
                            GUILayout.Label(secondLabel, GUILayout.Width(100));
                        }
                        GUILayout.EndHorizontal();
                    }
                }
                if (m_PersistentLoaders != null && m_PersistentLoaders.Count > 0)
                {
                    GUILayout.Label("<b>============================常驻资源============================</b>");
                    GUILayout.BeginHorizontal();
                    {
                        GUILayout.Label("<b>名字</b>");
                        GUILayout.Label("<b>引用数量</b>", GUILayout.Width(240));
                    }
                    GUILayout.EndHorizontal();
                    foreach (Loader loader in m_PersistentLoaders)
                    {
                        GUILayout.BeginHorizontal();
                        {
                            GUILayout.Label(loader.Path);
                            GUILayout.Label(loader.RefCount.ToString(), GUILayout.Width(240));
                        }
                        GUILayout.EndHorizontal();
                    }
                }
                if (needRefresh)
                {
                    TakeSample();
                }
            }
            GUILayout.EndVertical();
        }

        private void TakeSample()
        {
            var mLoadModule = LoadModule.Instance;
            m_AllLoaders = mLoadModule.GetAllLoader();
            m_TempLoaders = new List<Loader>();
            m_PersistentLoaders = new List<Loader>();
            switch (m_Type)
            {
                case PoolType.AssetBundle:
                    foreach (var kv in m_AllLoaders)
                    {
                        var loader = kv.Value;
                        if (!loader.IsAtlas)
                        {
                            if (loader.Persistent)
                            {
                                m_PersistentLoaders.Add(loader);
                            }
                            else
                            {
                                m_TempLoaders.Add(loader);
                            }
                        }
                    }
                    break;
                case PoolType.Atlas:
                    foreach (var kv in m_AllLoaders)
                    {
                        var loader = kv.Value;
                        if (loader.IsAtlas)
                        {
                            if (loader.Persistent)
                            {
                                m_PersistentLoaders.Add(loader);
                            }
                            else
                            {
                                m_TempLoaders.Add(loader);
                            }
                        }
                    }
                    break;
            }
        }
    }
}